package com.fashion.server.utils;

import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JedisUtil {
	private Logger logger = Logger.getLogger(JedisUtil.class);

	public JedisPool jedisPool;

    public Long del(String key)  throws Exception{
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.del(key);
        } catch (Exception e) {
        	e.printStackTrace();
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    
    public Object get(String key)  throws Exception{
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return ByteArrayHelper.byte2Object(jedis.get(key.getBytes()));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    
    public JedisPool getJedisPool()  throws Exception{
        return jedisPool;
    }

    
    public String getStr(String key) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.get(key);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    
    public Long hdel(String key, String field)  throws Exception{
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.hdel(key, field);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    
    public Object hget(String key, String field) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return ByteArrayHelper.byte2Object(jedis.hget(key.getBytes(), field.getBytes()));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    
    public Map<byte[], byte[]> hgetAll(String key)  throws Exception{
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.hgetAll(key.getBytes());
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    
    public Long hset(String key, String field, Object value, int seconds)  throws Exception{

        Jedis jedis = getJedisPool().getResource();
        try {
            Long ret = jedis.hset(key.getBytes(), field.getBytes(), ByteArrayHelper.object2Bytes(value));
            if (seconds > 0) {
                jedis.expire(key, seconds);
            }
            return ret;
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    
    public boolean isExist(String key)  throws Exception{
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.exists(key);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return false;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    
    public Object lindex(String key, int index)  throws Exception{
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return ByteArrayHelper.byte2Object(jedis.lindex(key.getBytes(), index));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    
    public Object lpop(String key)  throws Exception{
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            Object object = ByteArrayHelper.byte2Object(jedis.lpop(key.getBytes()));
            return object;
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }
    
    public String spop(String key)  throws Exception{
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return new String(jedis.spop(key.getBytes()));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    
    public void lpush(String key, Object value)  throws Exception{
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            jedis.lpush(key.getBytes(), ByteArrayHelper.object2Bytes(value));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    
    public List<Object> lrange(String key, int start, int end)  throws Exception{
        Jedis jedis = getJedisPool().getResource();
        List<Object> ret = new ArrayList<Object>();
        try {
            List<byte[]> list = jedis.lrange(key.getBytes(), start, end);
            for (byte[] i : list) {
                ret.add(ByteArrayHelper.byte2Object(i));
            }
            return ret;
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    
    public void lset(String key, int index, Object value) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            jedis.lset(key.getBytes(), index, ByteArrayHelper.object2Bytes(value));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    
    public void rpush(String key, Object value)  throws Exception{
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            jedis.rpush(key.getBytes(), ByteArrayHelper.object2Bytes(value));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }

    }

    
    public void set(String key, Object value) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            jedis.set(key.getBytes(), ByteArrayHelper.object2Bytes(value));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            getJedisPool().returnResource(jedis);
        }

    }

    
    public void set(String key, Object value, int seconds)  throws Exception{
        Jedis jedis = getJedisPool().getResource();
        try {
            jedis.setex(key.getBytes(), seconds, ByteArrayHelper.object2Bytes(value));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    
    public void setJedisPool(JedisPool jedisPool) throws Exception {
        this.jedisPool = jedisPool;
    }

    
    public void setStr(String key, String value) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            jedis.set(key, value);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    
    public String setexStr(String key, String value, int seconds) throws  Exception {
        Jedis jedis = getJedisPool().getResource();
        String res = null;
        try {
            res = jedis.setex(key,seconds,value);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            throw e;
        } finally {
            getJedisPool().returnResource(jedis);
        }
        return res;
    }

    
    public Long zadd(String key, double score, Object member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zadd(key.getBytes(), score, ByteArrayHelper.object2Bytes(member));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    
    public Long zcard(String key) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zcard(key.getBytes());
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    
    public Long zcount(String key, double min, double max)  throws Exception{
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zcount(key.getBytes(), min, max);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    
    public Long zrank(String key, Object member)  throws Exception{
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zrank(key.getBytes(), ByteArrayHelper.object2Bytes(member));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    
    public Long zrem(String key, Object member)  throws Exception{
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zrem(key.getBytes(), ByteArrayHelper.object2Bytes(member));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    
    public List<Object> zrevrange(String key, int start, int end)  throws Exception{
        Jedis jedis = getJedisPool().getResource();
        List<Object> returnSet = new ArrayList<Object>();
        try {
            Set<byte[]> set = jedis.zrevrange(key.getBytes(), start, end);
            for (byte[] s : set) {
                returnSet.add(ByteArrayHelper.byte2Object(s));
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return returnSet;
    }

    
    public Long zrevrank(String key, Object member)  throws Exception{
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zrevrank(key.getBytes(), ByteArrayHelper.object2Bytes(member)) == null ? -1 : jedis.zrevrank(
                    key.getBytes(), ByteArrayHelper.object2Bytes(member));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }
    
    
	public long incr(String key) throws Exception{
		Jedis jedis = getJedisPool().getResource();
		try {
			return jedis.incr(key);
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
			return -1l;
		} finally {
			getJedisPool().returnResource(jedis);
		}
	}

	
	public long incrBy(String key, long integer) throws Exception{
		Jedis jedis = getJedisPool().getResource();
		try {
			return jedis.incrBy(key, integer);
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
			return -1l;
		} finally {
			getJedisPool().returnResource(jedis);
		}
	}

	public long expire(String key, int seconds) throws Exception{
		Jedis jedis = getJedisPool().getResource();
		try {
			return jedis.expire(key, seconds);
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
			return -1l;
		} finally {
			getJedisPool().returnResource(jedis);
		}
	}

    public long decr( String key) throws Exception{
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.decr(key);
        } catch (Exception e) {
            logger.error("redis incrBy err：", e);
            return 0;
        }finally {
            getJedisPool().returnResource(jedis);
        }
    }

    /**
     * REDIS自减指定值
     *
     * @param key
     * @param integer
     * @return
     */
    public long decrBy( String key, long integer) throws Exception{
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.decrBy(key, integer);
        } catch (Exception e) {
            logger.error("redis incrBy err：", e);
            return 0;
        }finally {
            getJedisPool().returnResource(jedis);
        }
    }
}
